import React from 'react'
import { Redirect, Route } from 'react-router-dom'
import { useSelector } from 'react-redux'

const AuthRoute = ({ path, component: Component, ...rest }) => {
  const token = useSelector((state) => state.login.token)
  return (
    <Route
      path={path}
      {...rest}
      render={(props) => {
        if (token) {
          return <Component {...props} />
        } else {
          console.log('重定向了')
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { pathname: props.location.pathname }
              }}
            />
          )
        }
      }}></Route>
  )
}
export default AuthRoute
